﻿// WinIo_Usage.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <windows.h>
#include <stdio.h>
#include "WinIo/include/winio.h"

#pragma intrinsic(memcpy)

bool ReadPhysMem(DWORD64 pbPhysAddr, PVOID buffer, size_t size);
bool WritePhysMem(DWORD64 pbPhysAddr, PVOID buffer, size_t size);

int main()
{
    BOOL isSuccess = FALSE;

    if (!InitializeWinIo())
    {
        printf("InitializeWinIo failed!\r\n");
        return -1;
    }

    BYTE byteBuf[0x100] = { 0 };
    ReadPhysMem(0xFED80400, byteBuf, _countof(byteBuf));

    for (int i = 0; i < _countof(byteBuf); i++)
    {
        printf("%0.2X ", byteBuf[i]);
    }
    printf("\n");

    ShutdownWinIo();

    return 0;
}

bool ReadPhysMem(DWORD64 pbPhysAddr, PVOID buffer, size_t size)
{
    tagPhysStruct PhysStruct = { 0 };
    PhysStruct.pvPhysAddress = pbPhysAddr;
    PhysStruct.dwPhysMemSizeInBytes = size;

    PVOID LinAdr = MapPhysToLin(PhysStruct);
    if (NULL == LinAdr)
    {
        return false;
    }

    memcpy(buffer, LinAdr, size);

    UnmapPhysicalMemory(PhysStruct);

    return true;
}

bool WritePhysMem(DWORD64 pbPhysAddr, PVOID buffer, size_t size)
{
    tagPhysStruct PhysStruct = { 0 };
    PhysStruct.pvPhysAddress = pbPhysAddr;
    PhysStruct.dwPhysMemSizeInBytes = size;

    PVOID LinAdr = MapPhysToLin(PhysStruct);
    if (NULL == LinAdr)
    {
        return false;
    }

    memcpy(LinAdr, buffer, size);

    UnmapPhysicalMemory(PhysStruct);

    return true;
}